//
//  KBTabbar.m
//  KBTabbarController
//
//  Created by kangbing on 16/5/31.
//  Copyright © 2016年 kangbing. All rights reserved.
//

#import "KBTabbar.h"

@implementation KBTabbar


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.flag = -1;
        NSLog(@"initTabBar");
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"确认事项"] forState:UIControlStateNormal];
        btn.bounds = CGRectMake(0, 0, 64, 64);
        self.centerBtn = btn;
        [self addSubview:btn];
        
        self.barTintColor = [UIColor colorWithRed:203/255.0 green:172/255.0 blue:116/255.0 alpha:1.0];
        self.tintColor = [UIColor blackColor];

        self.layer.shadowColor = [UIColor colorWithRed:113/255.0 green:72/255.0 blue:11/255.0 alpha:0.3].CGColor;
        self.layer.shadowOffset = CGSizeMake(0,0);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 10;
    }
    return self;
}

+(void)load{
    // 获取当前类的tabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
    
    NSMutableDictionary *mNormalDic = [[NSMutableDictionary alloc]initWithDictionary:[item titleTextAttributesForState:UIControlStateNormal]];
    mNormalDic[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    mNormalDic[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:mNormalDic forState:UIControlStateNormal];

}

- (void)layoutSubviews
{
    
    [super layoutSubviews];
    if (self.flag == -1) {
        self.flag = self.safeAreaInsets.bottom;
    }
    NSLog(@"hahahaha:%@",@(self.safeAreaInsets.bottom));
////    if (self.safeAreaInsets.bottom==0) {
    self.centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * (self.flag==0?0.25:0.1));
        
        int index = 0;
        CGFloat wigth = self.bounds.size.width / 5;
        
        for (UIView* sub in self.subviews) {
            if (self.safeAreaInsets.bottom > self.flag) {
                sub.hidden = YES;
            }else{
                sub.hidden = NO;
            }
//            sub.hidden = self.safeAreaInsets.bottom!=0;
            if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                
                sub.frame = CGRectMake(index * wigth, self.bounds.origin.y, wigth, sub.frame.size.height);
                
                index++;
                
                if (index == 2) {
                    index++;
                }
            }
        }
//    }
}
 /*
- (void)layoutSubviews
{
    
    [super layoutSubviews];
    
    self.centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.1);
    
    int index = 0;
    CGFloat wigth = self.bounds.size.width / 5;
    
    for (UIView* sub in self.subviews) {
        
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            sub.frame = CGRectMake(index * wigth, self.bounds.origin.y, wigth, sub.frame.size.height);
            
            index++;
            
            if (index == 2) {
                index++;
            }
        }
    }
}
  */
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (self.isHidden == NO) {
        
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        
        if ( [self.centerBtn pointInside:newPoint withEvent:event]) {
            return self.centerBtn;
        }else{
            
            return [super hitTest:point withEvent:event];
        }
    }
    
    else {
        return [super hitTest:point withEvent:event];
    }
}



@end
